<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>第5章 JavaScript——基于对象的语言</title>
</head>
<body>
<script>
    var myDate = new Date();
    document.write(myDate.toISOString());

    myDate = new Date('1 Jan 2014');
    document.write(myDate.toLocaleDateString());

    var myNumber = 23;
    document.write(myNumber.toString());

    var myArrayRef = [0, 1, 2];
    var mySecondArrayRef = myArrayRef;
    myArrayRef[0] = 100;
    alert(mySecondArrayRef[0]);

    var myVariable = 'ABC';
    var mySecondVariable = myVariable;
    myVariable = "DEF";
    alert(myVariable);
    alert(mySecondVariable);

    var myArray = [];
    myVariable = myArray.length;
    alert(myVariable);
    myArray.length = 12;
    alert(myArray.length);

    myArray.sort();

    var myString = "I'm a String object";
    var lengthOfString = myString.length;
    document.write(lengthOfString.toString());
    var mySecondString = "I'm a primitive string";

    var theResult = '23' + 23;
    document.write(theResult);

    var lengthOfSecondString = mySecondString.length;
    document.write(lengthOfSecondString.toString());

    var string1 = 'Hello';
    var string2 = '123';
    var string3 = '123.456';
    console.log('%s', string1, string2, string3);

    var myName = 'Jeremy';
    document.write(myName.length.toString());

    var jeremy = 'Hello jeremy. How are you Jeremy';
    var foundAtPosition = jeremy.indexOf('Jeremy');
    alert(foundAtPosition);

    jeremy = 'Hello Jeremy. How are you Jeremy';
    foundAtPosition = jeremy.lastIndexOf('Jeremy');
    alert(foundAtPosition);

    myString = 'JavaScirpt';
    var mySubString = myString.substring(0, 4);
    alert(mySubString);

    mySubString = myString.substr(0, 4);
    alert(mySubString);

    var fileName = window.location.href;
    fileName = fileName.substr(fileName.lastIndexOf('/') + 1);
    document.write('The file name of this page is ' + fileName);

    myString = "I Dont't Care About Case";
    if (myString.toLowerCase() == "i don't care about case") {
        alert('Who care albut case?');
    }

    myString = prompt('Enter some text', 'Hello World!');
    var theLastChar = myString.charAt(myString.length - 1);
    document.write('The last character is ' + theLastChar);

    myString = String.fromCharCode(65, 66, 67);

    for (var i = 65; i <= 90; i++) {
        myString += String.fromCharCode(i);
    }
    document.write(myString);

    var name = prompt('Please enter your name');
    name = name.trim();
    alert('Hello, ' + name);

    var names = [];
    names[0] = 'Paul';
    names[1] = 'Jeremy';
    names[11] = 'Nick';
    document.write('The last name is ' + names[names.length - 1]);

    names = [];
    names.push('Jeremy');
    names.push('Paul');

    names = ['Paul', 'Jeremy', 'Nick'];
    var ages = [31, 30, 31];
    var concatArray = names.concat(ages);
    document.write(concatArray.toString());

    names = ['Paul', 'Sarah', 'Jeremy', 'Adam', 'Bob'];
    var sliceArray = names.slice(1, 3);
    document.write(sliceArray.toString());

    var myShopping = ['Eggs', 'Milk', 'Potatoes', 'Cereal', 'Banana'];
    var myShoppingList = myShopping.join('<br/>');
    document.write(myShoppingList);

    names.sort();
    document.write('Now the names again in order <br/>');
    for (i = 0; i < names.length; i++) {
        document.write(names[i] + '<br/>');
    }
    for (i = 0; i < 5; i++) {
        document.write(names[i] + '<br/>');
    }

    myShopping.reverse();
    myShoppingList = myShopping.join('<br/>');
    document.write(myShoppingList);

    var colors = ['red', 'blue', 'green', 'blue'];
    alert(colors.indexOf('read'));
    alert(colors.lastIndexOf('blue'));

    function functionName(value, index, array) {
        //        do something here
    }

    var numbers = [1, 2, 3, 4, 5];
    function isLessThan3(value, index, array) {
        var returnValue = false;
        if (value < 3) {
            returnValue = true;
        }
        return returnValue;
    }
    alert(numbers.every(isLessThan3));
    alert(numbers.some(isLessThan3));

    if (numbers.some(isLessThan3)) {
        var result = numbers.filter(isLessThan3);
        alert('These numbers are less than 3: ' + result);
    }

    for (i = 0; i < numbers.length; i++) {
        result = numbers[i] * 2;
        alert(result);
    }

    function doubleAndAlert(value, index, array) {
        var result = value * 2;
        alert(result);
    }
    numbers.forEach(doubleAndAlert);

    function doubleAndReturn(value, index, array) {
        return value * 2;
    }
    var doubledNumbers = numbers.map(doubleAndReturn);
    alert('The doubled numbers are: ' + doubledNumbers);

    var radius = prompt('Give the radius of the circle', '');
    var area = Math.PI * radius * radius;
    document.write('The area is ' + area);

    myNumber = -101;
    document.write(Math.abs(myNumber).toString());

    var max = Math.max(21, 22);
    var min = Math.min(30.1, 30.2);
    document.write(max.toString());
    document.write(min.toString());

    myNumber = 101.01;
    document.write(Math.ceil(myNumber) + '<br/>');
    document.write(parseInt(myNumber, 10).toString());

    myNumber = 44.5;
    document.write(Math.round(myNumber) + '<br/>');

    myNumber = 44.49;
    document.write(Math.round(myNumber).toString());

    var diceThrow;
    for (i = 0; i < 10; i++) {
        diceThrow = Math.floor(Math.random() * 6) + 1;
        document.write(diceThrow + '<br/>');
    }

    var firstNumber = 123;
    var secondNumber = 123;
    console.log('%s', firstNumber, secondNumber);
    myNumber = 123.765;
    document.write(myNumber.toString());
    myNumber = '123.567';
    document.write(myNumber.toString());

    var itemCost = 9.99;
    console.log(itemCost);
    var itemCostAfterTax = 9.99 * 1.075;
    document.write('Item cost is $' + itemCostAfterTax + '<br/>');
    itemCostAfterTax = itemCostAfterTax.toFixed(2);
    document.write('Item const fixed to 2 decimal places is $' + itemCostAfterTax);

    var theDate = new Date();
    var theDate2 = new Date(949278 * 1000000);
    var theDate3 = new Date('31 January 2014');
    var theDate4 = new Date(2014, 0, 31, 15, 35, 20, 20);
    document.write(theDate.toLocaleString());
    document.write(theDate2.toLocaleString());
    document.write(theDate3.toLocaleString());
    document.write(theDate4.toLocaleString());

    var myDateObject = new Date();
    var theMonth = myDateObject.getMonth();
    document.write(theMonth.toLocaleString());

    myDateObject.setFullYear(2018);
    myDateObject.setDate(27);
    myDateObject.setMonth(1);

    myDate = new Date('1 Jan 2010');
    mydate.setDate(32);
    document.write(myDate.toLocaleString());

    var nowDate = new Date();
    var currentDay = nowDate.getDate();
    nowDate.setDate(currentDay + 28);
    nowDate.setDate(currentDay - 28);

    nowDate.setHours(9);
    nowDate.setMinutes(57);
    alert(nowDate);
    nowDate.setMinutes(64);
    alert(nowDate);

    var firstName = 'John';
    var lastName = 'Doe';
    document.write(firstName + '`' + lastName);

    var johnDoe = {};
    johnDoe.firstName = 'John';
    johnDoe.lastName = 'Doe';
    johnDoe.greet = function () {
        alert('My name is ' + this.firstName + ' ' + this.lastName);
    };

    johnDoe.greet();

    johnDoe = {
        firstName: 'John',
        lastName: 'Doe',
        greet: function () {
            alert('My name is ' + this.firstName + ' ' + this.lastName);
        }
    };

    function Person(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    Person.prototype.getFullName = function () {
        return this.firstName + ' ' + this.lastName;
    };

    Person.prototype.greet = function (person) {
        alert('Hello, ' + person.getFullName() + '. I\'m ' + this.getFullName());
    };

    var person1 = new Person('P', 'J');
    var person2 = new Person('L', 'J');
    var areSame = person1.getFullName() == person2.getFullName();
    console.log(areSame);

    johnDoe = new Person('John', 'Doe');
    janeDoe = new Person('Jane', 'Doe');
    janeDoe.greet(johnDoe);
</script>
</body>
</html>